//
//  UIBarButtonItem+Item.m
//  BaiSiBuDeJie-Laki
//
//  Created by Laki on 16/4/2.
//  Copyright © 2016年 xiaomage. All rights reserved.
//

#import "UIBarButtonItem+Item.h"

@implementation UIBarButtonItem (Item)
#pragma mark --------------------
#pragma mark 创建UIBarButtonItem普通和高亮状态
+ (UIBarButtonItem *)itemImage:(UIImage *)image withHighImage:(UIImage *)HighImage addTarget:(nullable id)target action:(SEL)action{
    //创建一个自定义按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    //设置按钮普通状态和高亮状态的图片
    [btn setImage:image forState:UIControlStateNormal];
    [btn setImage:HighImage forState:UIControlStateHighlighted];
    
    //设置按钮自适应
    [btn sizeToFit];
    
    //监听按钮点击事件
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    //创建一个空的View包装一个按钮,initWithCustomView方法会增加按钮的点击范围
    UIView *view = [[UIView alloc] initWithFrame:btn.bounds];
    [view addSubview:btn];
    
    //返回一个创建好的UIBarButtonItem
    return [[UIBarButtonItem alloc] initWithCustomView:view];
}

#pragma mark --------------------
#pragma mark 创建UIBarButtonItem普通和选中状态
+ (UIBarButtonItem *)itemImage:(UIImage *)image withSelectImage:(UIImage *)selectImage addTarget:(nullable id)target action:(SEL)action{
    //创建一个自定义按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    //设置按钮普通状态和选中状态的图片
    [btn setImage:image forState:UIControlStateNormal];
    [btn setImage:selectImage forState:UIControlStateSelected];
    
    //设置按钮自适应
    [btn sizeToFit];
    
    //监听按钮点击事件
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    //创建一个空的View包装一个按钮,initWithCustomView方法会增加按钮的点击范围
    UIView *view = [[UIView alloc] initWithFrame:btn.bounds];
    [view addSubview:btn];
    
    //返回一个创建好的UIBarButtonItem
    return [[UIBarButtonItem alloc] initWithCustomView:view];
}
@end
